Skip to content

Improve api for Caveat Enforcer actions and Client#50

Merged
hanzel98 merged 19 commits into
mainfrom
cursor/accept-delegation-object-and-move-client-export-9bcd
Aug 8, 2025
Merged

Improve api for Caveat Enforcer actions and Client#50
hanzel98 merged 19 commits into
mainfrom
cursor/accept-delegation-object-and-move-client-export-9bcd

Conversation

@jeffsmale90

@jeffsmale90 jeffsmale90 commented Aug 7, 2025

Copy link
Copy Markdown
Collaborator

📝 Description

This PR refactors the caveat enforcer actions and client within the @metamask/delegation-toolkit package to simplify their API and improve developer experience.

Note: This PR was authored with Cursor Agent.

🔄 What Changed?

  • Simplified Action Parameters: All caveat enforcer actions (e.g., getMultiTokenPeriodEnforcerAvailableAmount) now exclusively accept a single parameter: { delegation: Delegation }.
  • Removed Explicit Address Parameters: delegationManager and enforcerAddress are no longer accepted as explicit parameters; they are always derived from the environment object. This simplifies the interface, and allows for this functionality to be added in the future.
  • Moved client factory to main export: createCaveatEnforcerClient and CaveatEnforcerClient are now exported to the main API export at @metamask/delegation-toolkit while the individual actions and the actions factory are exported at @metamask/delegation-toolkit/actions

🚀 Why?

  • Improved Developer Experience: Callers no longer need to manually hash delegations, extract caveat terms/args, or resolve enforcer/delegation manager addresses. The API is significantly more ergonomic, requiring only the Delegation object.
  • Simplified API: A single, consistent parameter type across all caveat enforcer actions reduces cognitive load and improves predictability.
  • Centralized Client Access: Moving the CaveatEnforcerClient to the main package export makes it more discoverable and directly accessible.
  • Standard Practices: Adopting standard Error objects aligns with common JavaScript error handling patterns.

🧪 How to Test?

  • Manual testing steps:
    1. Import createCaveatEnforcerClient from @metamask/delegation-toolkit and verify it can be instantiated.
    2. Call any caveat enforcer action (e.g., client.getMultiTokenPeriodEnforcerAvailableAmount) passing only a { delegation: Delegation } object and confirm correct results.
    3. Test scenarios where a delegation contains no matching caveat or multiple matching caveats for a specific enforcer, ensuring a standard Error with a descriptive message is thrown.
  • Automated tests added/updated
  • All existing tests pass

⚠️ Breaking Changes

  • Breaking changes (describe below):
    • The API for all caveat enforcer actions has changed. Callers must now pass a { delegation: Delegation } object. The previous parameters (delegationHash, terms, args, delegationManager, enforcerAddress) are no longer accepted.

This is only a breaking change in development, as these interfaces have not been released.

📋 Checklist

  • Code follows the project's coding standards
  • Self-review completed
  • Documentation updated (if needed)
  • Tests added/updated
  • Changelog updated (if needed)
  • All CI checks pass

🔗 Related Issues

Closes #
Related to #

📚 Additional Notes

This PR is the result of several iterations to refine the API based on feedback, leading to a much cleaner and more intuitive interface for interacting with caveat enforcer contracts.


Open in Cursor Open in Web

- Add new action functions that accept entire delegation instead of delegation hash and args
- Add findMatchingCaveat utility function to extract caveat data from delegation
- Add NoMatchingCaveatError and MultipleMatchingCaveatsError for error handling
- Add comprehensive test coverage for new functionality
- Maintain backward compatibility with existing action functions
@cursor

cursor Bot commented Aug 7, 2025

Copy link
Copy Markdown

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@jeffsmale90
jeffsmale90 marked this pull request as ready for review August 7, 2025 00:04
@jeffsmale90
jeffsmale90 requested a review from a team as a code owner August 7, 2025 00:04
Comment thread packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts
cursor[bot]

This comment was marked as outdated.

Comment thread packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts Outdated
Comment thread packages/delegation-toolkit/src/actions/index.ts Outdated
@jeffsmale90 jeffsmale90 changed the title Accept delegation object and move client export Improve api for Caveat Enforcer actions and Client Aug 7, 2025
@jeffsmale90
jeffsmale90 force-pushed the cursor/accept-delegation-object-and-move-client-export-9bcd branch from 98eab59 to 5c2e0a5 Compare August 7, 2025 01:38
…hDelegation directly. Use type assertion when destructuring foundCaveats.
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@jeffsmale90
jeffsmale90 force-pushed the cursor/accept-delegation-object-and-move-client-export-9bcd branch 2 times, most recently from 5592f5d to c4fa6d4 Compare August 7, 2025 02:08
@jeffsmale90
jeffsmale90 force-pushed the cursor/accept-delegation-object-and-move-client-export-9bcd branch from c4fa6d4 to 89628de Compare August 7, 2025 02:38
Comment thread packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts Outdated
Comment thread packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts
Comment thread packages/delegation-toolkit/src/index.ts Outdated
Comment thread packages/delegation-toolkit/test/actions/caveatEnforcerClient.test.ts Outdated
@jeffsmale90
jeffsmale90 force-pushed the cursor/accept-delegation-object-and-move-client-export-9bcd branch from 0b1a78e to 0b03514 Compare August 8, 2025 00:33
Comment thread packages/delegation-toolkit/src/actions/caveatEnforcerClient.ts
@hanzel98
hanzel98 merged commit a6caae9 into main Aug 8, 2025
32 of 39 checks passed
@hanzel98
hanzel98 deleted the cursor/accept-delegation-object-and-move-client-export-9bcd branch August 8, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants